package com.example.testproject.activity

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.example.testproject.R
import com.example.testproject.databinding.HiltTestDataBinDing
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.components.SingletonComponent
import javax.inject.Inject
import javax.inject.Qualifier

/**
 *
 * @ClassName: HiltTestActivity
 * @Author: android 超级兵
 * @CreateDate: 12/20/21$ 10:58 AM$
 * TODO jetPack组件:hilt 使用
 * 参考文档:https://juejin.cn/post/6970580755520946213
 */
@AndroidEntryPoint
class HiltTestActivity : AppCompatActivity() {
    lateinit var dataBinDing: HiltTestDataBinDing

    @Inject
    lateinit var person1: Person

    @Inject
    lateinit var person2: Person

    @Inject
    lateinit var person3: Person

    // SingletonComponent
    @SingleStudentAnnotation
    @Inject
    lateinit var singleStudent1: String

    @SingleStudent2Annotation
    @Inject
    lateinit var singleStudent2: String

    @SingleStudent3Annotation
    @Inject
    lateinit var singleStudent3: String
//
//    @Inject lateinit var singleStudent21: String
//    @Inject lateinit var singleStudent22: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        dataBinDing =
            DataBindingUtil.setContentView(this, R.layout.activity_hilt_test)

        dataBinDing.tv1.text = person1.show1()
        dataBinDing.tv2.text =
            "singleStudent1:$singleStudent1\n" +
                    "singleStudent2:$singleStudent2\n" +
                    "singleStudent3:${singleStudent3}"
    }
}

class Person @Inject constructor() {
    fun show1(): String = "Person:show1"
}


/*
 * 作者:android 超级兵
 * 创建时间: 12/20/21 1:19 PM
 * TODO
 *  SingletonComponent
 *      - 跟随 Application 生命周期
 *      - 作用范围:Application#onCreate - Application-onDestroy
 *  ActivityRetainedComponent
 *      - 跟随Activity生命周期 [如果旋转屏幕导致activity重建,该组件不会销毁,简单的说和ViewModel生命周期一致]
 *      - 作用范围:ViewModel Activity Fragment View
 *  ActivityComponent
 *      - 和 Activity 生命周期一致
 *      - 作用范围:Activity Fragment View
 *  ViewModelComponent
 *      - 只能作用在ViewModel上,其他功能和 ActivityRetainedComponent 一致
 *  FragmentComponent
 *      - 只能作用在Fragment上
 *      - 作用范围:Fragment#onAttach - Fragment#onDestroy
 *  ViewComponent
 *      - 只能作用在View上
 *  ViewWithFragmentComponent
        - ViewWithFragmentComponent也是针对View的，
          但是注入的时候不仅要求在View上加入@AndroidEntryPoint，还要加上@WithFragmentBindings
          安装在它上面的模块的生命周期也是与ViewComponent一样的。其中提供的依赖只能用在View上，
          而且这个View还只能用在Fragment中，不能用在Activity中。
 *  ServiceComponent
 *      - 只能作用在Service上
 * */
@Module
@InstallIn(SingletonComponent::class)
class SingleStudent {

    @SingleStudentAnnotation
    @Provides
    fun show1(): String = "SingleStudent:show1~~~"

    @SingleStudent3Annotation
    @Provides
    fun getPerson(person: Person) = person.show1()
}


@Module
@InstallIn(SingletonComponent::class)
object SingleStudent2 {

    @SingleStudent2Annotation
    @Provides
    fun show1(): String = "SingleStudent2:show1@@@"
}

@Qualifier
annotation class SingleStudentAnnotation

@Qualifier
annotation class SingleStudent2Annotation

@Qualifier
annotation class SingleStudent3Annotation
